package com.mis.pos.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Description 跨域配置
 * @Author yuxiang
 * @CreatedDate 2019/11/18 21:45
 */
@Configuration
public class CorsConfig {

    private CorsConfiguration buildConfig(){
        //跨域配置类
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许任何域名使用
        corsConfiguration.addAllowedOrigin("*");
        //允许任何头
        corsConfiguration.addAllowedHeader("*");
        //允许任何方法（post get put delete等）
        corsConfiguration.addAllowedMethod("*");
        //已经授权的头
        corsConfiguration.addExposedHeader("Authorization");
        return corsConfiguration;
    }

    //使用过滤器的方式
    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //对接口配置跨域设置
        source.registerCorsConfiguration("/**",buildConfig());
        return new CorsFilter(source);
    }
}
